package com.vca.service.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.vca.common.constants.Constants;
import com.vca.common.constants.OrderRedisConstans;
import com.vca.common.exception.VcaException;
import com.vca.common.model.course.Course;
import com.vca.common.model.course.CoursePackage;
import com.vca.common.model.course.CoursePackageRelation;
import com.vca.common.model.exhibition.Exhibition;
import com.vca.common.model.lineUp.VcaLineUp;
import com.vca.common.model.sms.SmsTemplate;
import com.vca.common.model.system.SystemNotification;
import com.vca.common.model.talk.Talk;
import com.vca.common.model.user.User;
import com.vca.common.model.user.UserToken;
import com.vca.common.request.PageParamRequest;
import com.vca.common.response.LineUpResponse;
import com.vca.common.utils.DateUtil;
import com.vca.common.utils.PageUtils;
import com.vca.common.utils.RedisUtil;
import com.vca.common.utils.VcaUtil;
import com.vca.common.vo.LineUpVo;
import com.vca.common.vo.dateLimitUtilVo;
import com.vca.service.dao.lineUp.VcaLineUpDao;
import com.vca.service.service.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 排队登记表 服务实现类
 * </p>
 *
 * @author chenBing
 * @since 2022-11-24
 */
@Service
public class VcaLineUpServiceImpl extends ServiceImpl<VcaLineUpDao, VcaLineUp> implements VcaLineUpService {

    private static final Logger logger = LoggerFactory.getLogger(VcaLineUpServiceImpl.class);

    @Autowired
    private UserService userService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private CoursePackageService coursePackageService;

    @Autowired
    private CoursePackageRelationService coursePackageRelationService;

    @Autowired
    private TalkService talkService;

    @Autowired
    private ExhibitionService exhibitionService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private TemplateMessageService templateMessageService;

    @Autowired
    private UserTokenService userTokenService;

    @Autowired
    private SystemNotificationService systemNotificationService;

    @Autowired
    private SmsService smsService;

    @Autowired
    private SmsTemplateService smsTemplateService;

    @Autowired
    private VcaOrderService vcaOrderService;

    /**
     * @Description:排队登记（不选择排期）
     * @author chenbing
     * @date 2022/11/28 10:11
     */
    @Override
    public boolean lineUp(Long mainId, Integer type) {
        User user = userService.getInfoException();
        VcaLineUp lineUp = getOne((new LambdaQueryWrapper<VcaLineUp>().eq(VcaLineUp::getMainId, mainId).eq(VcaLineUp::getUserId, user.getUid()).eq(VcaLineUp::getType, type).eq(VcaLineUp::getIsDeleted, 0)));
        if (lineUp != null) {
            lineUp.setIsDeleted(0);
            lineUp.setCreateTime(new Date());
            lineUp.setIsTip(0);
            lineUp.setNoticeCount(0);
            lineUp.setNoticeStatus(0);
            lineUp.setNoticeTime(null);
            UpdateWrapper<VcaLineUp> updateWrapper = new UpdateWrapper<VcaLineUp>().eq("main_id", mainId).eq("user_id", user.getUid())
                    .eq("type", type).eq("is_deleted", 0).set("create_time", new Date()).set("is_tip", 0)
                    .set("notice_count", 0).set("notice_status", 0).set("notice_time", null);
            return update(updateWrapper);
        }
        VcaLineUp vcaLineUp = new VcaLineUp();
        vcaLineUp.setUserId(user.getUid());
        vcaLineUp.setType(type);
        vcaLineUp.setMainId(mainId);
        vcaLineUp.setNoticeStatus(0);
        vcaLineUp.setNoticeCount(0);
        vcaLineUp.setIsDeleted(0);
        vcaLineUp.setCreateTime(DateUtil.nowDateTime());
        return save(vcaLineUp);
    }

    /**
     * @Description:候补名单列表
     * @author:chenbing
     * @date 2022/12/21 16:07
     */
    @Override
    public PageInfo<LineUpResponse> getList(String language,PageParamRequest pageParamRequest) {
        User user = userService.getInfo();
        List<VcaLineUp> lineUpList = list(new LambdaQueryWrapper<VcaLineUp>().eq(VcaLineUp::getIsDeleted, 0).eq(VcaLineUp::getUserId, user.getUid()));
        List<LineUpResponse> lineUpResponses = new ArrayList<>();
        for (VcaLineUp vcaLineUp : lineUpList) {
            LineUpResponse lineUpResponse = new LineUpResponse();
            lineUpResponse.setLineUpId(vcaLineUp.getId());
            lineUpResponse.setMainType(vcaLineUp.getType());
            lineUpResponse.setMainId(vcaLineUp.getMainId());
            lineUpResponse.setNoticeStatus(vcaLineUp.getNoticeStatus());
            switch (vcaLineUp.getType()) {
                case 0:
                    if("en".equals(language)){
                        lineUpResponse.setTagText("Courses");
                    }else{
                        lineUpResponse.setTagText("课程");
                    }

                    Course course = courseService.getById(vcaLineUp.getMainId());
                    // TODO: 2023/1/6 查询课程是否还有空余席位
                    lineUpResponse.setSeatsLeft(courseService.info(language,course.getId()).getTotalSeatsLeftForAllScheduledCourses() > 0);
                    lineUpResponse.setImagePath(course.getCover());
                    if("en".equals(language)){
                        lineUpResponse.setName(course.getNameEn());
                    }else{
                        lineUpResponse.setName(course.getName());
                    }
                    lineUpResponse.setThemeColor(categoryService.getById(course.getCourseTypeId()).getLabelColor());
                    lineUpResponse.setStatus(course.getStatus());
                    lineUpResponse.setIsDeleted(course.getIsDeleted() == 1);
                    break;
                case 1:
                    if("en".equals(language)){
                        lineUpResponse.setTagText("Group Course");
                    }else{
                        lineUpResponse.setTagText("套课");
                    }

                    CoursePackage coursePackage = coursePackageService.getById(vcaLineUp.getMainId());
                    // TODO: 2023/1/6 查询课程是否还有空余席位
                    lineUpResponse.setSeatsLeft(courseService.packageInfo(language,coursePackage.getId()).getTotalSeatsLeftForAllScheduledCourses() > 0);
                    lineUpResponse.setImagePath(coursePackage.getCoverImage());
                    if("en".equals(language)){
                        lineUpResponse.setName(coursePackage.getNameEn());
                    }else{
                        lineUpResponse.setName(coursePackage.getName());
                    }
                    lineUpResponse.setThemeColor(categoryService.getById(coursePackage.getCourseTypeId()).getLabelColor());
                    lineUpResponse.setStatus(coursePackage.getStatus() == 1);
                    lineUpResponse.setIsDeleted(coursePackage.getIsDeleted() == 1);
                    break;
                case 2:
                    if("en".equals(language)){
                        lineUpResponse.setTagText("Talks");
                    }else{
                        lineUpResponse.setTagText("讲座");
                    }

                    Talk talk = talkService.getById(vcaLineUp.getMainId());
                    // TODO: 2023/1/6 查询讲座是否还有空余席位
                    lineUpResponse.setSeatsLeft(talkService.info(language,talk.getId()).getTotalSeatsLeftForAllScheduledTalk() > 0);
                    lineUpResponse.setImagePath(talk.getCoverImage());
                    if("en".equals(language)){
                        lineUpResponse.setName(talk.getNameEn());
                    }else{
                        lineUpResponse.setName(talk.getName());
                    }
                    lineUpResponse.setThemeColor("#B4B19E");
                    lineUpResponse.setStatus(talk.getStatus() == 1);
                    lineUpResponse.setIsDeleted(talk.getIsDeleted() == 1);
                    break;
                case 3:
                    if("en".equals(language)){
                        lineUpResponse.setTagText("Exhibitions");
                    }else{
                        lineUpResponse.setTagText("展览");
                    }

                    Exhibition exhibition = exhibitionService.getById(vcaLineUp.getMainId());
                    // TODO: 2023/1/6 查询展览是否还有空余席位
                    lineUpResponse.setSeatsLeft(exhibitionService.info(language,exhibition.getId()).getTotalSeatsLeftForAllScheduledExhibition() > 0);
                    lineUpResponse.setImagePath(exhibition.getCover());

                    if("en".equals(language)){
                        lineUpResponse.setName(exhibition.getNameEn());
                    }else{
                        lineUpResponse.setName(exhibition.getName());
                    }

                    lineUpResponse.setThemeColor("#B4B19E");
                    lineUpResponse.setStartTime(DateUtil.dateToStr(exhibition.getStartTime(), Constants.DATE_FORMAT_DATE));
                    lineUpResponse.setEndTime(DateUtil.dateToStr(exhibition.getEndTime(), Constants.DATE_FORMAT_DATE));
                    lineUpResponse.setStatus(exhibition.getStatus() == 1);
                    lineUpResponse.setIsDeleted(exhibition.getIsDeleted() == 1);
                    break;
                default:
                    break;
            }
            lineUpResponses.add(lineUpResponse);
        }
        return PageUtils.startPage(lineUpResponses, pageParamRequest.getPage(), pageParamRequest.getLimit());
    }

    /**
     * @param keyword      用户昵称或用户ID
     * @param type         类型 0=课程 1=套课 2=讲座 3=展览 4=全部
     * @param noticeStatus 通知状态 0=未通知 1=已通知 2=全部
     * @param dateLimit    申请时间区间（yyyy-MM-dd,yyyy-MM-dd）
     * @param request      分页参数
     * @Description:排队管理列表
     * @Author: chenBing
     * @Date: 2022/11/25
     */
    @Override
    public PageInfo<LineUpVo> getLineUps(String keyword, Integer type, Integer noticeStatus, String dateLimit, PageParamRequest request) {
        LambdaQueryWrapper<VcaLineUp> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)) {
            if (VcaUtil.isString2Num(keyword)) {
                lqw.eq(VcaLineUp::getUserId, keyword);
            } else {
                List<User> users = userService.list(new LambdaQueryWrapper<User>().like(User::getNickname, keyword));
                if (users.size() > 0) {
                    List<Integer> userIds = users.stream().map(User::getUid).collect(Collectors.toList());
                    lqw.in(VcaLineUp::getUserId, userIds);
                } else {
                    return new PageInfo<>();
                }
            }
        }
        if (ObjectUtil.isNotNull(type)) {
            if (type != 4) {
                lqw.eq(VcaLineUp::getType, type);
            }
        }
        if (ObjectUtil.isNotNull(noticeStatus)) {
            if (noticeStatus != 2) {
                lqw.eq(VcaLineUp::getNoticeStatus, noticeStatus);
            }
        }
        if (StringUtils.isNotBlank(dateLimit)) {
            dateLimitUtilVo dateLimitUtilVo = DateUtil.getDateLimit(dateLimit);
            lqw.between(VcaLineUp::getCreateTime, dateLimitUtilVo.getStartTime(), dateLimitUtilVo.getEndTime());
        }
        List<VcaLineUp> lineUps = list(lqw);
        List<LineUpVo> lineUpVos = new ArrayList<>();
        lineUps.forEach(lineUp -> {
            LineUpVo lineUpVo = new LineUpVo();
            BeanUtils.copyProperties(lineUp, lineUpVo);
            User user = userService.getById(lineUp.getUserId());
            if (user != null) {
                lineUpVo.setNickName(ObjectUtil.isNotNull(user.getNickname()) ? user.getNickname() : null);
            }
            lineUpVo = changeLineUpVo(lineUp.getType(), lineUpVo, lineUp);
            lineUpVo.setNoticeStatus(lineUp.getNoticeStatus() == 0 ? "未通知" : "已通知");
            lineUpVos.add(lineUpVo);
        });
        return PageUtils.startPage(lineUpVos, request.getPage(), request.getLimit());
    }

    /**
     * @Description:获取登记类型
     * @Author: chenBing
     * @Date: 2022/11/25
     */
    private LineUpVo changeLineUpVo(Integer type, LineUpVo lineUpVo, VcaLineUp lineUp) {
        String typeName = "";
        String applicationInformation = null;
        switch (type) {
            case 0:
                typeName = "课程";
                Course course = courseService.getById(lineUp.getMainId());
                applicationInformation = StringUtils.isNotBlank(course.getName()) ? course.getName() : null;
                break;
            case 1:
                typeName = "套课";
                CoursePackage coursePackage = coursePackageService.getById(lineUp.getMainId());
                applicationInformation = StringUtils.isNotBlank(coursePackage.getName()) ? coursePackage.getName() : null;
                break;
            case 2:
                typeName = "讲座";
                Talk talk = talkService.getById(lineUp.getMainId());
                applicationInformation = StringUtils.isNotBlank(talk.getName()) ? talk.getName() : null;
                break;
            case 3:
                typeName = "展览";
                Exhibition exhibition = exhibitionService.getById(lineUp.getMainId());
                applicationInformation = StringUtils.isNotBlank(exhibition.getName()) ? exhibition.getName() : null;
                break;
            default:
                break;
        }
        lineUpVo.setApplicationInformation(applicationInformation);
        lineUpVo.setType(typeName);
        return lineUpVo;
    }

    /**
     * @Description:通知
     * @Author: chenBing
     * @Date: 2022/11/25
     */
    @Override
    public boolean notice(String lineUpIds) {
        List<String> stringList = Arrays.asList(lineUpIds.split(","));
        List<VcaLineUp> vcaLineUps = new ArrayList<>();
        stringList.forEach(s -> {
            VcaLineUp lineUp = getById(Long.valueOf(s));
            lineUp.setNoticeCount(lineUp.getNoticeCount() + 1);
            lineUp.setNoticeStatus(1);
            lineUp.setIsTip(0);
            lineUp.setNoticeTime(DateUtil.nowDateTime());
            vcaLineUps.add(lineUp);
            redisUtil.lPush(OrderRedisConstans.LINE_UP_NOTICE, lineUp.getId());
        });
        return updateBatchById(vcaLineUps);
    }

    @Override
    public void noticeByMainId(Long mainId, Integer type) {
        List<VcaLineUp> lineUpList = list(new LambdaQueryWrapper<VcaLineUp>().eq(VcaLineUp::getMainId, mainId).eq(VcaLineUp::getType, type));
        for (VcaLineUp vcaLineUp : lineUpList) {
            redisUtil.lPush(OrderRedisConstans.LINE_UP_NOTICE, vcaLineUp.getId());
        }
    }

    /**
     * @Description:不再显示
     * @author:chenbing
     * @date 2022/12/21 17:29
     */
    @Override
    public Boolean disable(Integer lineUpId) {
        VcaLineUp vcaLineUp = getOne(new LambdaQueryWrapper<VcaLineUp>().eq(VcaLineUp::getId, lineUpId).eq(VcaLineUp::getUserId, userService.getUserId()));
        if (vcaLineUp == null) {
            throw new VcaException("当前排队信息不存在");
        }
        vcaLineUp.setIsTip(1);
        return updateById(vcaLineUp);
    }

    /**
     * @Description:排队状态通知生成模板消息
     * @author:chenbing
     * @date 2022/12/30 13:32
     */
    @Override
    public void generateLineUpNotice() {
        String redisKey = OrderRedisConstans.LINE_UP_NOTICE;
        Long size = redisUtil.getListSize(redisKey);
        logger.info("VcaLineUpServiceImpl.generateLineUpNotice | size:" + size);
        if (size < 1) {
            return;
        }
        for (int i = 0; i < size; i++) {
            //如果10秒钟拿不到一个数据，那么退出循环
            Object data = redisUtil.getRightPop(redisKey, 10L);
            if (ObjectUtil.isNull(data)) {
                continue;
            }
            try {
                VcaLineUp lineUp = getById(Integer.valueOf(String.valueOf(data)));
                if (ObjectUtil.isNull(lineUp)) {
                    logger.error("VcaLineUpServiceImpl.generateLineUpNotice | lineUp不存在，lineUp: " + data);
                    throw new VcaException("lineUp不存在，lineUp: " + data);
                }
                Boolean result = generateLineUpNotice(lineUp);
                logger.info("VcaLineUpServiceImpl.generateLineUpNotice.result====>{}", result);
                if (!result) {
                    redisUtil.lPush(redisKey, data);
                }
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("Exception==>{}", e.getMessage());
//                throw new VcaException(e.getMessage());
                redisUtil.lPush(redisKey, data);
            }
        }
    }

    /**
     * @Description:根据排队状态通知生成模板消息
     * @author:chenbing
     * @date 2022/12/30 14:00
     */
    private Boolean generateLineUpNotice(VcaLineUp lineUp) {
        lineUp.setNoticeCount(lineUp.getNoticeCount() + 1);
        lineUp.setNoticeStatus(1);
        lineUp.setIsTip(0);
        lineUp.setNoticeTime(DateUtil.nowDateTime());
        updateById(lineUp);
        SystemNotification paySuccessOfEvent = systemNotificationService.getByMark(OrderRedisConstans.QUEUE_STATUS);
        if (paySuccessOfEvent.getIsRoutine().equals(1)) {
            generateLineUpNoticeRoutine(lineUp, paySuccessOfEvent.getRoutineId());
        }
        if (paySuccessOfEvent.getIsWechat().equals(1)) {
            // TODO: 2023/1/3 公众号模板消息
            generateLineUpNoticePublic(lineUp, paySuccessOfEvent.getWechatId());
        }
        if (paySuccessOfEvent.getIsSms().equals(1)) {
            // TODO: 2023/1/3 发送短信
            SmsTemplate smsTemplate = smsTemplateService.getDetail(paySuccessOfEvent.getSmsId());
            generateLineUpNoticeSms(lineUp, smsTemplate);
        }
        return Boolean.TRUE;
    }

    private Boolean generateLineUpNoticePublic(VcaLineUp lineUp, Integer wechatId) {
        HashMap<String, Object> message = new HashMap<>();
        message.put("first", "恭喜您，排队的课程已预约成功");
        String pageUrl = "";
        HashMap<String, Object> addressInfoMap = new HashMap<>();
        message.put("keyword3", DateUtil.nowDate(Constants.DATE_FORMAT));
        message.put("keyword4", "廖老师");
        switch (lineUp.getType()) {
            // TODO: 2022/12/30 课程
            case 0:
                Course course = courseService.getById(lineUp.getMainId());
                message.put("keyword1", course.getName());
                addressInfoMap = vcaOrderService.getAddressInfoMap("",course.getCourseAddressId());
                Object address = addressInfoMap.get("courseAddress") + " " + addressInfoMap.get("courseAddressInfo");
                message.put("keyword2", address);
                pageUrl = choosePackage(course.getId(), 0);
                message.put("remark", "您候补的课程已有空位，请尽快完成预约");
                break;
            // TODO: 2022/12/30 套课
            case 1:
                CoursePackage coursePackage = coursePackageService.getById(lineUp.getMainId());
                List<CoursePackageRelation> packageRelationList = coursePackageRelationService.list(new LambdaQueryWrapper<CoursePackageRelation>()
                        .eq(CoursePackageRelation::getCoursePackageId, coursePackage.getId()));
                List<Long> courseIds = packageRelationList.stream().map(CoursePackageRelation::getCourseId).collect(Collectors.toList());
                List<Course> courses = courseService.listByIds(courseIds);
                String addresss = "";
                if (courses.size() > 0) {
                    List<Integer> addressIds = courses.stream().map(Course::getCourseAddressId).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
                    for (int i = 0; i < addressIds.size(); i++) {
                        addressInfoMap = vcaOrderService.getAddressInfoMap("",addressIds.get(i));
                        addresss += addressInfoMap.get("courseAddress") + " " + addressInfoMap.get("courseAddressInfo");
                        if (i == addressIds.size() - 1) {
                            addresss += ",";
                        }
                    }
                }
                message.put("keyword1", coursePackage.getName());
                message.put("keyword2", addresss);
                pageUrl = choosePackage(coursePackage.getId(), 1);
                message.put("remark", "您候补的套课已有空位，请尽快完成预约");
                break;
            // TODO: 2022/12/30 讲座
            case 2:
                Talk talk = talkService.getById(lineUp.getMainId());
                message.put("keyword1", talk.getName());
                addressInfoMap = vcaOrderService.getAddressInfoMap("",talk.getAddressId());
                message.put("keyword2", addressInfoMap.get("courseAddress") + " " + addressInfoMap.get("courseAddressInfo"));
                if (talk.getTalkType() == 2) {
                    message.put("keyword2", "微信小程序");
                }
                pageUrl = choosePackage(talk.getId(), 3);
                message.put("remark", "您候补的讲座已有空位，请尽快完成预约");
                break;
            // TODO: 2022/12/30 展览
            case 3:
                Exhibition exhibition = exhibitionService.getById(lineUp.getMainId());
                message.put("keyword1", exhibition.getName());
                addressInfoMap = vcaOrderService.getAddressInfoMap("",exhibition.getCourseAddressId());
                message.put("keyword2", addressInfoMap.get("courseAddress") + " " + addressInfoMap.get("courseAddressInfo"));
                pageUrl = choosePackage(exhibition.getId(), 3);
                message.put("remark", "您候补的展览已有空位，请尽快完成预约");
                break;
            default:
                break;
        }
        UserToken userToken = userTokenService.getTokenByUserIdException(lineUp.getUserId(), OrderRedisConstans.SYSTEM_TYPE_ROUTINE);
        if (userToken != null) {
            templateMessageService.pushTemplatePublicMessage(pageUrl, wechatId, message, userToken.getToken());
        }
        return Boolean.TRUE;
    }

    private Boolean generateLineUpNoticeSms(VcaLineUp lineUp, SmsTemplate smsTemplate) {
        String name = null;
        String pageUrl = "";
        switch (lineUp.getType()) {
            // TODO: 2022/12/30 课程
            case 0:
                Course course = courseService.getById(lineUp.getMainId());
                name = course.getName();
                pageUrl = choosePackage(course.getId(), 0);
                break;
            // TODO: 2022/12/30 套课
            case 1:
                CoursePackage coursePackage = coursePackageService.getById(lineUp.getMainId());
                name = coursePackage.getName();
                pageUrl = choosePackage(coursePackage.getId(), 1);
                break;
            // TODO: 2022/12/30 讲座
            case 2:
                Talk talk = talkService.getById(lineUp.getMainId());
                name = talk.getName();
                pageUrl = choosePackage(talk.getId(), 2);
                break;
            // TODO: 2022/12/30 展览
            case 3:
                Exhibition exhibition = exhibitionService.getById(lineUp.getMainId());
                name = exhibition.getName();
                pageUrl = choosePackage(exhibition.getId(), 3);
                break;
            default:
                break;
        }
        User user = userService.getById(lineUp.getUserId());
        if (ObjectUtil.isNotNull(user.getPhone())) {
            smsService.sendLineUp(name, user.getPhone(), smsTemplate.getTempId(), pageUrl);
        }
        return Boolean.TRUE;
    }

    /**
     * @Description:根据排队状态通知生成小程序模板消息
     * @author:chenbing
     * @date 2022/12/30 13:36
     */
    private Boolean generateLineUpNoticeRoutine(VcaLineUp lineUp, Integer routineId) {
        //课程名称{{thing1.DATA}}课程时间{{time2.DATA}}温馨提示{{thing3.DATA}}
        //您排队的已有空位，请尽快前往小程序预约
        HashMap<String, String> messageMap = new HashMap<>();
        String pageUrl = "";
        switch (lineUp.getType()) {
            // TODO: 2022/12/30 课程
            case 0:
                Course course = courseService.getById(lineUp.getMainId());
                messageMap.put("thing1", course.getName());
                messageMap.put("time2", DateUtil.nowDate(Constants.DATE_FORMAT_DATE_STRING));
                pageUrl = choosePackage(course.getId(), 0);
                messageMap.put("thing3", "您的候补已有空位释出，点击前往预约。");
                break;
            // TODO: 2022/12/30 套课
            case 1:
                CoursePackage coursePackage = coursePackageService.getById(lineUp.getMainId());
                messageMap.put("thing1", coursePackage.getName());
                messageMap.put("time2", DateUtil.nowDate(Constants.DATE_FORMAT_DATE_STRING));
                pageUrl = choosePackage(coursePackage.getId(), 1);
                messageMap.put("thing3", "您的候补已有空位释出，点击前往预约。");
                break;
            // TODO: 2022/12/30 讲座
            case 2:
                Talk talk = talkService.getById(lineUp.getMainId());
                messageMap.put("thing1", talk.getName());
                messageMap.put("time2", DateUtil.nowDate(Constants.DATE_FORMAT_DATE_STRING));
                pageUrl = choosePackage(talk.getId(), 2);
                messageMap.put("thing3", "您的候补已有空位释出，点击前往预约。");
                break;
            // TODO: 2022/12/30 展览
            case 3:
                Exhibition exhibition = exhibitionService.getById(lineUp.getMainId());
                messageMap.put("thing1", exhibition.getName());
                messageMap.put("time2", DateUtil.nowDate(Constants.DATE_FORMAT_DATE_STRING));
                messageMap.put("thing3", "您的候补已有空位释出，点击前往预约。");
                pageUrl = choosePackage(exhibition.getId(), 3);
                break;
            default:
                break;
        }
        UserToken userToken = userTokenService.getTokenByUserIdException(lineUp.getUserId(), OrderRedisConstans.SYSTEM_TYPE_ROUTINE);
        if (userToken != null) {
            templateMessageService.pushMiniTemplateMessage(routineId, messageMap, userToken.getToken(), pageUrl);
        }
        return Boolean.TRUE;
    }

    public String choosePackage(Object id, Integer type) {
        String url = "";
        switch (type) {
            case 0:
                url = "pages/course-detail/index?id=" + id;
                break;
            case 1:
                url = "pages/bundlecourse-detail/index?id=" + id;
                break;
            case 2:
                url = "pages/talk-detail/index?id=" + id;
                break;
            case 3:
                url = "pages/exhibition-detail/index?id=" + id;
                break;
            default:
                break;
        }
        return url;
    }

    /**
     * @Description:移除排队记录
     * @author:chenbing
     * @date 2023/1/3 16:49
     */
    @Override
    public boolean removeLineUpById(Integer lineUpId) {
        if (count(new LambdaQueryWrapper<VcaLineUp>().eq(VcaLineUp::getUserId, userService.getUserId()).eq(VcaLineUp::getId, lineUpId)) <= 0) {
            throw new VcaException("当前排队记录不存在");
        }
        return removeById(lineUpId);
    }

    @Override
    public void initLineUp() {
        update(new UpdateWrapper<VcaLineUp>().eq("is_tip", 1).eq("is_deleted", 0).set("is_tip", 0));
    }
}
